<?php 

class Event_StaticPageVersionPreValidation extends Crud_Event_Handler 
{
	protected $_type = 'preValidation';
	
	
	/**
	 * Run handler with options array.
	 * 
	 * @param OrdersRow $row
	 * @return void
	 */
	public function run($row) {
		
		if ($this->_getParam('leave_page')) {
			$this->_exit($row);
		}
		
		if ($this->_getParam('approved_page')) {
			$staticPageRow = null;
			if ($row instanceof Crud_Section_Row) {
				$staticPageRow = $row->getRow();
			}
			
			$versionPage = PagesService::getInstance()->getActualVersionStaticPage($staticPageRow);
			//проверка полного апрува, и в случае успеха копирование версии в страницу
			if (PagesService::getInstance()->checkApproveComplete($versionPage)) {
				$versionPage = PagesService::getInstance()->setApprovedVersionAndCopyVersion($versionPage, $staticPageRow);
			}
			
			$versionPage->save();
			$this->_exit($row);
		}
		
	}
	
	protected function _exit($row)
	{
		PagesService::getInstance()->exitFromAdminRow($row, $this->_getParam('action'), $this->_getParam('table'));
	}
	
}
